<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="选手姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入选手姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国籍" prop="country">
        <el-input
          v-model="queryParams.country"
          placeholder="请输入国籍"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small " @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="small " @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:pInformation:add']"
        >新增报名</el-button>
      </el-col>
      <el-col :span="1.5">
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-show=" userName == 'admin'"
        type="danger"
        plain
        icon="el-icon-delete"
        size="mini"
        :disabled="multiple"
        @click="handleDelete"
        v-hasPermi="['system:pInformation:remove']"
      >批量删除</el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>-->
    </el-row>

    <el-table border v-loading="loading" :data="pInformationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column  v-if="this.$store.state.user.name == 'admin'" label="用户名" align="center" prop="createUsername" />
      <el-table-column label="选手姓名" align="center" prop="name" >
        <template slot-scope="scope">
          <el-link  style="color: blue" @click="goDetail(scope.row)">{{scope.row.name}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="个人证件照" align="center" prop="imageName" width="150px" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.imageName" :width="100" :height="100"/>
        </template>
      </el-table-column>
      <el-table-column label="国籍" align="center" prop="country" />
      <el-table-column label="所在赛区" align="center" prop="locatedArea" >
        <template slot-scope="scope">
          {{scope.row.locatedArea == '1' ? '北京赛区':scope.row.locatedArea == '2' ? '北京延庆赛区':scope.row.locatedArea == '3' ? '张家口赛区':'--' }}
        </template>
      </el-table-column>
      <el-table-column label="比赛场地" align="center" prop="competitionVenue" :formatter="formatterCompetitionVenue"/>
      <el-table-column label="项目名称" align="center" prop="projectName" :formatter="projectNameFormatter" >
      </el-table-column>
      <el-table-column label="项目种类" align="center" prop="projectType">
        <template slot-scope="scope">
          {{scope.row.projectType == 1 ? '单人赛':scope.row.projectType == 2 ? '双人赛': scope.row.projectType == 3 ?'团体赛':'--' }}
        </template>
      </el-table-column>
      <el-table-column label="报名时间" prop="createTime"/>
      <el-table-column label="审核状态" align="center" prop="auditStatus" :formatter="formAuditStatus"/>
      <el-table-column label="拒绝原因" align="center" prop="refuseReason" >
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus === 2">
            {{scope.row.refuseReason}}
         </span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="250px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          {{scope.row.auditStatus}}-->
          <el-button
            :disabled="scope.row.auditStatus == null ? false : true"
            size="small"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:pInformation:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="warning"
            icon="el-icon-document-checked"
            @click="aduit(scope.row)"
            v-hasPermi="['system:pInformation:audit']"
          >审核</el-button>
          <el-button
            v-show=" userName == 'admin'"
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:pInformation:remove']"
          >删除</el-button>
          <el-button
            :disabled="scope.row.auditStatus == null  ? false : true"
            v-show=" userName != 'admin'"
            type="warning"
            icon="el-icon-delete"
            size="small"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:pInformation:remove']"
          >取消报名</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报名信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item v-if="this.$store.state.user.name == 'admin'" label="用户名" prop="createUsername">
          <el-input v-model="form.createUsername" placeholder="请输入选手用户名" style="width: 220px"/>
        </el-form-item>
        <el-form-item label="选手姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入选手姓名" style="width: 220px"/>
        </el-form-item>
        <el-form-item label="个人证件照" prop="imageName">
          <image-upload v-model="form.imageName"/>
        </el-form-item>
        <el-form-item label="国籍" prop="country">
          <el-input v-model="form.country" placeholder="请输入国籍" style="width: 220px"/>
        </el-form-item>
        <el-form-item label="所在赛区" prop="locatedArea">
          <el-select v-model="form.locatedArea" placeholder="请选择赛区">
            <el-option
              v-for="item in locatedAreaList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="比赛场地" prop="competitionVenue">
          <el-select
            v-model="form.competitionVenue"
            placeholder="请选择比赛场地">
            <el-option
              v-for="item in competitionVenueList"
              :value="item.value"
              :label="item.label"
              :key="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-select v-model="form.projectName" placeholder="请选择项目">
            <el-option
              v-for="dict in projectNameList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="项目种类" prop="projectType">
          <el-select v-model="form.projectType" placeholder="请选择项目种类">
            <el-option
              v-for="dict in projectTypeList1"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="auditTitle" :visible.sync="openStatus" width="650px" append-to-body>
      <el-form ref="formAudit" :model="formAudit" :rules="rulesAudit" label-width="80px">
        <el-form-item :label="'审核结果'" prop="auditStatus">
          <el-radio-group v-model="formAudit.auditStatus" name="audit_form_status">
            <el-radio :label="1">{{'审核通过' }}</el-radio>
            <el-radio :label="2">{{'审核不通过'}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="formAudit.auditStatus == 2" :label="'审核拒绝原因'" prop="refuseReason" >
          <el-input v-model="formAudit.refuseReason" name="audit_form_refusalCause" :rows="4" type="textarea" maxlength="120" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAduit">提交审核</el-button>
        <el-button @click="cancelAduit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPInformation, getPInformation, delPInformation, addPInformation, updatePInformation ,auditPInformation} from "@/api/system/pInformation";

export default {
  name: "PInformation",
  dicts: ['sys_notice_type'],
  data() {
    return {
      userName:this.$store.state.user.name,
      openStatus:false,
      auditTitle:"审核",
      formAudit:{},
      locatedAreaList:[
        {
          value: '1',
          label:'北京赛区'
        },
        {
          value: '2',
          label:'北京延庆赛区'
        },
        {
          value: '3',
          label:'张家口赛区'
        }
      ],
      competitionVenueList:[
        {
          value: '1',
          label: '首都体育馆'
        },
        {
          value: '2',
          label: '国家速滑馆'
        },
        {
          value: '3',
          label: '国家体育馆'
        },
        {
          value: '4',
          label: '五棵松体育中心'
        },
        {
          value: '5',
          label: '国家游泳中心'
        },
        {
          value: '6',
          label: '首钢滑雪大跳台中心'
        },
        {
          value: '7',
          label: '国家雪车雪橇中心'
        },
        {
          value: '8',
          label: '国家高山滑雪中心'
        },
        {
          value: '9',
          label: '云顶滑雪公园'
        },
        {
          value: '10',
          label: '国家跳台滑雪中心'
        },
        {
          value: '11',
          label: '国家越野滑雪中心'
        },
        {
          value: '12',
          label: '国家冬季两项中心'
        }
      ],
      rulesAudit:{
        auditStatus: [
          { required: true, message: "审核状态不能为空", trigger: "blur" }
        ],
        refuseReason: [
          { required: true, message: "审核拒绝理由不能为空", trigger: "blur" }
        ]
      },
      projectNameList: [
      {
        value: '1',
        label: '短道速滑'
      }, {
        value: '2',
        label: '速度滑冰'
      }, {
        value: '3',
        label: '花样滑冰'
      }, {
        value: '4',
        label: '冰球'
      }, {
        value: '5',
        label: '冰壶'
      }, {
        value: '6',
        label: '雪车'
      }, {
        value: '7',
        label: '钢架雪车'
      }, {
        value: '8',
        label: '雪橇'
      }, {
        value: '9',
        label: '冬季两项'
      }, {
        value: '10',
        label: '高山滑雪'
      }, {
        value: '11',
        label: '自由式滑雪'
      }, {
        value: '12',
        label: '单板滑雪'
      }, {
        value: '13',
        label: '越野滑雪'
      }, {
        value: '14',
        label: '跳台滑雪'
      },{
        value: '15',
        label: '北欧两项'
      }
    ],
      projectTypeList1:[
        {
          value: 1,
          label: '单人赛'
        },
        {
          value: 2,
          label: '双人赛'
        },
        {
          value: 3,
          label: '团体赛'
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报名信息表格数据
      pInformationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        country: null,
        projectName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createUsername: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        imageName: [
          { required: true, message: "个人证件照不能为空", trigger: "blur" }
        ],
        projectType: [
          { required: true, message: "项目类型不能为空", trigger: "change" }
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        competitionVenue: [
          { required: true, message: "比赛场地不能为空", trigger: "blur" }
        ],
        locatedArea: [
          { required: true, message: "比赛赛区不能为空", trigger: "blur" }
        ],
        country: [
          { required: true, message: "国籍不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "选手姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //比赛场地
    formatterCompetitionVenue(row,cloumn){
      switch(row.competitionVenue){
        case '1':
          return '首都体育馆';
          break;
        case '2':
          return '国家速滑馆';
          break;
        case '3':
          return '国家体育馆';
          break;
        case '4':
          return '五棵松体育中心';
          break;
        case '5':
          return '国家游泳中心';
          break;
        case '6':
          return '首钢滑雪大跳台中心';
          break;
        case '7':
          return '国家雪车雪橇中心';
          break;
        case '8':
          return '国家高山滑雪中心';
          break;
        case '9':
          return '云顶滑雪公园';
          break;
        case '10':
          return '国家跳台滑雪中心';
          break;
        case '11':
          return '国家越野滑雪中心';
          break;
        case '12':
          return '国家冬季两项中心';
          break;
        default:
          return '未知';
      }
    },
    // 项目名称
    projectNameFormatter(row,cloumn){
      switch(row.projectName){
        case '1':
          return '短道速滑';
          break;
        case '2':
          return '速度滑冰';
          break;
        case '3':
          return '花样滑冰';
          break;
        case '4':
          return '冰球';
          break;
        case '5':
          return '冰壶';
          break;
        case '6':
          return '雪车';
          break;
        case '7':
          return '钢架雪车';
          break;
        case '8':
          return '雪橇';
          break;
        case '9':
          return '冬季两项';
          break;
        case '10':
          return '高山滑雪';
          break;
        case '11':
          return '自由式滑雪';
          break;
        case '12':
          return '单板滑雪';
          break;
        case '13':
          return '跳台滑雪';
          break;
        case '14':
          return '越野滑雪';
          break;
        case '15':
          return '北欧两项';
          break;
        default:
          return '未知';
      }
    },
    /**
     * 提交审核
     */
    submitFormAduit(){
      const self = this
      self.$refs["formAudit"].validate(valid => {
        if (valid) {
          if (self.formAudit.id != null) {
            debugger
            auditPInformation(self.formAudit).then(response => {
              self.$modal.msgSuccess("提交成功");
              self.openStatus = false;
              self.getList();
            });
          }
        }
      });
    },
    aduit(row){
      debugger
      const self = this
      if(row.auditStatus == null){
        self.formAudit.id = row.id
        self.openStatus = true
      }else if(row.auditStatus == 1) {
        self.formAudit.id = row.id
        self.formAudit.auditStatus = row.auditStatus
        self.openStatus = true
      }else if(row.auditStatus == 2){
        self.formAudit.id = row.id
        self.formAudit.auditStatus = row.auditStatus
        self.formAudit.refuseReason = row.refuseReason
        self.openStatus = true
      }


    },
    cancelAduit() {
      this.openStatus = false;
      this.resetAduit();
    },
    resetAduit() {
      this.formAudit= {
        auditStatus:null,
        refuseReason:null
      };
      this.resetForm("formAudit");
    },
    formAuditStatus(row){
      switch (row.auditStatus) {
        case 1:
          return '审核通过';
          break;
        case 2:
          return '审核拒绝';
          break;
        default:
          return '待审核';
          break ;
      }
    },
    goDetail(row){
      if(row.auditStatus == 2 || row.auditStatus == null ){
        return
      }else{
        this.$router.push({name:'apliationDetail',query:{name:row.name,id:row.id}})
      }

    },

    /** 查询报名信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.createUsername = this.$store.state.user.name;
      listPInformation(this.queryParams).then(response => {
        this.pInformationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        country: null,
        projectName: null,
        projectType: null,
        extField: null,
        extField1: null,
        extField2: null,
        isDeleted: null,
        createUserid: null,
        createUsername: null,
        createTime: null,
        serverIp: null,
        updateUserid: null,
        updateUsername: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报名信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPInformation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报名信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        debugger
       const self = this
        if (valid) {
          if (this.form.id != null) {
            this.form.updateUsername = this.$store.state.user.name;
            updatePInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            debugger
            if(self.$store.state.user.name !='admin'){
              self.form.createUsername = self.$store.state.user.name;
            }
            addPInformation(self.form).then(response => {
              this.$modal.msgSuccess("报名成功，请等待审核");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if(this.userName =='admin'){
        this.$modal.confirm('是否确认删除报名信息编号为"' + ids + '"的数据项？').then(function() {
          return delPInformation(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }else if(this.userName !='admin'){
        this.$modal.confirm('是否确认取消报名信息编号为"' + ids + '"的数据项？').then(function() {
          return delPInformation(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消成功");
        }).catch(() => {});
      }

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/pInformation/export', {
        ...this.queryParams
      }, `pInformation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
